/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import modelo.DAO.PracticaLaboralDao;
import modelo.IdiomaOferta;
import modelo.PracticaLaboral;

/**
 *
 * @author Carlos Jose Mendoza
 */
public class ControlPracticaLaboral extends HttpServlet {
    
            PracticaLaboralDao prac = new PracticaLaboralDao();

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
       // try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
        /*
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ControlPracticaLaboral</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ControlPracticaLaboral at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
        */
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
      //  if (request.getServletPath().equals("/ConsultarOferta")){
         if(request.getServletPath().equals("/ConsultarOfertaIndividualEstudiante")) {
             try {
            PracticaLaboral practica2 = null;
            practica2 = prac.consultarPractica(request.getParameter("idOferta"));
            HttpSession sesion2 = request.getSession();
            sesion2.setAttribute("oferta", practica2);
            response.sendRedirect("/proyectoHojaDeVida/vistas/enlaces/vistasEstudiantes/consultarOfertaIndividual.jsp");
             }catch (Exception ex){
              throw new ExceptionInInitializerError(ex.getMessage());
             }
            
         }else { if(request.getServletPath().equals("/ConsultarOfertaIndividualAdministrador")){
             PracticaLaboral practica3 = null;
            practica3 = prac.consultarPractica(request.getParameter("idOferta"));
            HttpSession sesion3 = request.getSession();
            sesion3.setAttribute("oferta", practica3);
            response.sendRedirect("/proyectoHojaDeVida/vistas/enlaces/vistasAdministrador/consultarOfertaIndividual.jsp");
         } else {
            PracticaLaboral practica = null;
            practica = prac.consultarPractica(request.getParameter("idOferta"));
            HttpSession sesion = request.getSession();
            sesion.setAttribute("oferta", practica);
            response.sendRedirect("/proyectoHojaDeVida/vistas/enlaces/vistasEmpresas/consultarOfertaIndividual.jsp") ;
         }
         }
           
            
        //}
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
        PracticaLaboral practicaLaboral = new PracticaLaboral();
        HttpSession sesion = request.getSession();
        
        if(request.getServletPath().equals("/RegistrarPractica")) {
            
            int numeroVacante = Integer.parseInt(request.getParameter("numero_Vacantes"));
            practicaLaboral.setNumeroDeVacantes(numeroVacante);
            Calendar calendario = Calendar.getInstance();
            int dia = calendario.get(Calendar.DAY_OF_MONTH);
            int mes = (calendario.get(Calendar.MONTH) + 1);
            int año = calendario.get(Calendar.YEAR);
            String diaS = Integer.toString(dia);
            String MesS = Integer.toString(mes);
            String añoS = Integer.toString(año);
            practicaLaboral.setFechaPublicacion(""+diaS+"-"+MesS+"-"+añoS+"");
            practicaLaboral.setPrograma(request.getParameter("programa"));
            practicaLaboral.setSemestreRequerido(request.getParameter("semestre"));
            double valorRemuneracion = Double.parseDouble(request.getParameter("remuneracion"));
            practicaLaboral.setValorRemuneracion(valorRemuneracion);
            practicaLaboral.setDisponibilidad(request.getParameter("disponibilidad"));
            practicaLaboral.setSexo(request.getParameter("sexo"));
            
             String vecChar[] = new String[12];
            String fech = request.getParameter("fecha_limite");
            Character a = null;
            for(int i=0;i<fech.length();i++){
                a = fech.charAt(i);
                    vecChar[i] = a+"";
            }
                    
            String fechaTotal =vecChar[8] + vecChar[9] + vecChar[4]  + vecChar[5] + vecChar[6] + vecChar[7] + vecChar[0] + vecChar[1] +vecChar[2] + vecChar[3];
            
            practicaLaboral.setFechaLimite(fechaTotal);
            
            String vecCharUno[] = new String[12];
            String fech2 = request.getParameter("fecha_inicio");
            Character a2 = null;
            for(int i=0;i<fech2.length();i++){
                a2 = fech2.charAt(i);
                    vecCharUno[i] = a2+"";
            }
            String fechaTotal2 =vecCharUno[8] + vecCharUno[9] + vecCharUno[4]  + vecCharUno[5] + vecCharUno[6] + vecCharUno[7] + vecCharUno[0] + vecCharUno[1] +vecCharUno[2] + vecCharUno[3];
                   
           practicaLaboral.setFechaInicio(fechaTotal2);
           
           String vecChar3[] = new String[12];
            String fech3 = request.getParameter("fecha_final");
            Character a3 = null;
            for(int i=0;i<fech3.length();i++){
                a3 = fech3.charAt(i);
                    vecChar3[i] = a3+"";
            }
            String fechaTotal3 =vecChar3[8] + vecChar3[9] + vecChar3[4]  + vecChar3[5] + vecChar3[6] + vecChar3[7] + vecChar3[0] + vecChar3[1] +vecChar3[2] + vecChar3[3];
                   
            practicaLaboral.setFechaFinal(fechaTotal3);
            IdiomaOferta idio = new IdiomaOferta();
            idio.setNombre(request.getParameter("nombreIdioma"));
            idio.setDominio(request.getParameter("dominio"));
            practicaLaboral.setIdiomas(idio);
            practicaLaboral.setDescripcionOferta(request.getParameter("descripcionOferta"));
            boolean estado = prac.registrarPractica(practicaLaboral);
            if(estado)
                response.sendRedirect("/proyectoHojaDeVida/vistas/enlaces/vistasEmpresas/indexEmpresa.jsp");
            else
                response.sendRedirect("/proyectoHojaDeVida/vistas/enlaces/vistasEmpresas/Registrar_tipo_oferta.html");
            
        }
           
            
            
            
            
           // String cod = request.getParameter("idOferta");
           // practicaLaboral = prac.consultarPractica(cod);
           // sesion.setAttribute("oferta", practicaLaboral);
           // response.sendRedirect("/proyectoHojaDeVida/vistas/enlaces/vistasEstudiantes/ConsultarOfertaIndividual.jsp") ;
       
        
        //else if(request.getServletPath().equals("/ListarOfertas")) {
            
          //  List<PracticaLaboral> listaPractica =  prac.listarOfertas();
            //HttpSession sesion = request.getSession();
           // sesion.setAttribute("listaOferta", listaPractica);
           // response.sendRedirect("/proyectoHojaDeVida/vistas/enlaces/vistasEmpresas/indexEmpresa.jsp");
       // }
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
